package mi.top.utils;

import cn.hutool.core.date.DateUtil;
import org.eclipse.paho.client.mqttv3.MqttException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;


/**
 * 报警器
 *
 * @author 祥霸
 * @date 2021.07.13
 *
 * */
public class AlarmSwing implements CallBack{

    AlarmSwing alarmSwing;

    private JTextArea textArea;

    private JTextArea textArea2;

    private TextField productKey;
    private TextField deviceName;
    private TextField deviceSecret;

    private JButton init;

    /**
     * 01100010000501CF 开akc
     *
     * 01100000000501CF 关akc
     *
     */
    private String ackHex = "01100010000501CF";

    MqttUtils mqttUtils;

    public void init() {
        alarmSwing = this;

        JFrame frame = new JFrame();
        frame.setTitle("报警器");

        JPanel jp = new JPanel();

        JLabel label = new JLabel("productKey");
        productKey = new TextField(16);
        productKey.setText("");

        jp.add(label);
        jp.add(productKey);


        label = new JLabel("deviceName");
        deviceName = new TextField(16);
        deviceName.setText("");
        jp.add(label);
        jp.add(deviceName);

        label = new JLabel("deviceSecret");
        deviceSecret = new TextField(16);
        deviceSecret.setText("");
        jp.add(label);
        jp.add(deviceSecret);

        JButton button = new JButton("init");

        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("init:click");
                System.out.println("productKey：" + productKey.getText());
                System.out.println("deviceName：" + deviceName.getText());
                System.out.println("deviceSecret：" + deviceSecret.getText());

                synchronized (RasterSwing.class) {
                    if(mqttUtils != null){
                        return;
                    }

                    try {
                        System.out.println("MqttUtils:init");

                        mqttUtils = new MqttUtils(productKey.getText(), deviceName.getText(), deviceSecret.getText());
                        mqttUtils.init(mqttUtils.sign());

                        System.out.println("setBackground");

                        // 订阅消息
                        mqttUtils.subscribe(alarmSwing);

                        JOptionPane.showMessageDialog(null, "连接阿里云成功！");
                    } catch (MqttException ex) {
                        ex.printStackTrace();
                        System.out.println("连接失败");
                        JOptionPane.showMessageDialog(null, "连接阿里云失败！");
                    }
                }
                System.out.println("MqttUtils:init:end");
            }
        });

        jp.add(button);

        button = new JButton("ack");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("ack:click");
                try {
                    mqttUtils.publish("01100010000501CF");

                    appText2(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ": ack");

                } catch (MqttException ex) {
                    ex.printStackTrace();
                    System.out.println("ack:error");
                }

            }

        });
        jp.add(button);


        textArea = new JTextArea("",5,20);
        textArea.setLineWrap(true);
        textArea.setSize(300,300);
        JScrollPane scroll = new JScrollPane(textArea);
        //把定义的JTextArea放到JScrollPane里面去

        //分别设置水平和垂直滚动条自动出现
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setSize(300, 300);
        jp.add(scroll);

        JButton clean = new JButton("clean");

        clean.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
            }
        });
        jp.add(clean);


        textArea2 = new JTextArea("",5,20);
        textArea2.setLineWrap(true);
        textArea2.setSize(300,300);
        JScrollPane scroll2 = new JScrollPane(textArea2);
        //把定义的JTextArea放到JScrollPane里面去

        //分别设置水平和垂直滚动条自动出现
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setSize(300, 300);
        jp.add(scroll2);

        JButton clean2 = new JButton("clean");

        clean.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea2.setText("");
            }
        });
        jp.add(clean2);


        frame.add(jp);
        frame.setSize(300,500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    public JPanel getJPanel() {
        alarmSwing = this;

        JPanel jp = new JPanel();

        JLabel label = new JLabel("productKey");
        productKey = new TextField(16);
        productKey.setText("");

        jp.add(label);
        jp.add(productKey);


        label = new JLabel("deviceName");
        deviceName = new TextField(16);
        deviceName.setText("");
        jp.add(label);
        jp.add(deviceName);

        label = new JLabel("deviceSecret");
        deviceSecret = new TextField(16);
        deviceSecret.setText("");
        jp.add(label);
        jp.add(deviceSecret);

        init = new JButton("init");

        init.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("init:click");
                System.out.println("productKey：" + productKey.getText());
                System.out.println("deviceName：" + deviceName.getText());
                System.out.println("deviceSecret：" + deviceSecret.getText());

                synchronized (RasterSwing.class) {
                    if(mqttUtils != null){
                        return;
                    }

                    try {
                        System.out.println("MqttUtils:init");

                        mqttUtils = new MqttUtils(productKey.getText(), deviceName.getText(), deviceSecret.getText());
                        mqttUtils.init(mqttUtils.sign());

                        init.setBackground(new Color(0,0,0));

                        // 订阅消息
                        mqttUtils.subscribe(alarmSwing);

                        JOptionPane.showMessageDialog(null, "连接阿里云成功！");
                    } catch (MqttException ex) {
                        ex.printStackTrace();
                        System.out.println("连接失败");
                        JOptionPane.showMessageDialog(null, "连接阿里云失败！");
                    }
                }
                System.out.println("MqttUtils:init:end");
            }
        });

        jp.add(init);

        JButton button = new JButton("ack");
        button.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                System.out.println("ack:click");
                try {
                    mqttUtils.publish(ackHex);

                    appText2(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ": ack");

                } catch (MqttException ex) {
                    ex.printStackTrace();
                    System.out.println("ack:error");
                }

            }

        });
        jp.add(button);


        textArea = new JTextArea("",5,20);
        textArea.setLineWrap(true);
        textArea.setSize(300,300);
        JScrollPane scroll = new JScrollPane(textArea);
        //把定义的JTextArea放到JScrollPane里面去

        //分别设置水平和垂直滚动条自动出现
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setSize(300, 300);
        jp.add(scroll);

        JButton clean = new JButton("clean1");

        clean.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.setText("");
            }
        });
        jp.add(clean);


        textArea2 = new JTextArea("",5,20);
        textArea2.setLineWrap(true);
        textArea2.setSize(300,300);
        JScrollPane scroll2 = new JScrollPane(textArea2);
        //把定义的JTextArea放到JScrollPane里面去

        //分别设置水平和垂直滚动条自动出现
        scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        scroll.setSize(300, 300);
        jp.add(scroll2);

        JButton clean2 = new JButton("clean2");

        clean2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea2.setText("");
            }
        });
        jp.add(clean2);


        return jp;
    }

    public void appText(String txt) {
        textArea.append(txt);
        textArea.append("\n");
    }

    public void appText2(String txt) {
        textArea2.append(txt);
        textArea2.append("\n");
    }

    @Override
    public void msg(String msg) {
        String text = "";

        // 开
        if(msg.equals("0110001000050a0003001e010600010204226b")) {
            text = "开";
            this.ackHex = "01100010000501CF";
        }

        // 关
        else if(msg.equals("010600100000880f")) {
            text = "关";
            this.ackHex = "01100000000501CF";
        }
        // 未知内容
        else {
            text = "未知内容";
        }

        this.appText(DateUtil.format(new Date(), "yyyy-MM-dd HH:mm:ss") + ": " + text);
    }

}
